package cn.vcjf.weapp.api.goods.controller;


import cn.vcjf.weapp.api.goods.entity.GoodsDiscount;
import cn.vcjf.weapp.api.goods.entity.GoodsOrderItem;
import cn.vcjf.weapp.api.goods.mapper.GoodsOrderItemMapper;
import cn.vcjf.weapp.api.goods.service.GoodsDiscountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.goods.entity.GoodsOrder;
import cn.vcjf.weapp.api.goods.service.GoodsOrderService;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 订单
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/goods/goodsOrder")
public class GoodsOrderController {
	@Resource
	private GoodsOrderService goodsOrderService;
	@Resource
	private GoodsDiscountService goodsDiscountService;
	@Resource
	private GoodsOrderItemMapper goodsOrderItemMapper;
	/**
	 * 添加订单
	 * @param goodsOrder
	 * @return
	 */
	@RequestMapping("/addGoodsOrder")
	@ResponseBody
	public TableResult addGoodsOrder(GoodsOrder goodsOrder) {

		return goodsOrderService.addGoodsOrder(goodsOrder);
	}

	@RequestMapping("/updateGoodsOrder")
	@ResponseBody
	public TableResult updateGoodsOrder(GoodsOrder goodsOrder) {
		goodsOrder.preUpdate();
		boolean b = goodsOrderService.updateById(goodsOrder);
		return TableResult.Result(b);
	}

	/**
	 * 用户订单详情
 	 * @param goodsOrder
	 * @return
	 */
	@RequestMapping("/findUserOrderDetail")
	@ResponseBody
	public TableResult findUserOrderDetail(GoodsOrder goodsOrder) {

		EntityWrapper<GoodsOrder> wrapper = new EntityWrapper<GoodsOrder>();

		if (goodsOrder.getId() != null) {
			wrapper.eq("id", goodsOrder.getId());
		}
		if (StringUtils.isNotBlank(goodsOrder.getOrderNo())) {
			wrapper.eq("orderNo", goodsOrder.getOrderNo());
		}
		GoodsOrder result = goodsOrderService.selectOne(wrapper);

		GoodsOrderItem query = new GoodsOrderItem();
		query.setOrderNo(result.getOrderNo());
		//商品列表
		List<GoodsOrderItem> goodsOrderItems = goodsOrderItemMapper.selectOrderItemList(query);
		result.setOrderItems(goodsOrderItems);

		//优惠券
		GoodsDiscount goodsDiscount = goodsDiscountService.selectById(goodsOrder.getDiscountId());
		result.setGoodsDiscount(goodsDiscount);

		return TableResult.OKResult(result);
	}

	/**
	 * 用户订单列表
	 * @param goodsOrder
	 * @return
	 */
	@RequestMapping("/findUserOrderList")
	@ResponseBody
	public TableResult findGoodsOrderList(GoodsOrder goodsOrder) {

		goodsOrderService.updateExpireUserOrder(goodsOrder.getUserId());

		EntityWrapper<GoodsOrder> wrapper = goodsOrder.getQueryWrapper();
		Page<GoodsOrder> page = goodsOrder.getPageInfo();

		page = goodsOrderService.selectPage(page, wrapper);

		GoodsOrderItem query = new GoodsOrderItem();
		for(GoodsOrder result :page.getRecords()){
			query.setOrderNo(result.getOrderNo());
			//商品列表
			List<GoodsOrderItem> goodsOrderItems = goodsOrderItemMapper.selectOrderItemList(query);
			result.setOrderItems(goodsOrderItems);

			//优惠券
//			GoodsDiscount goodsDiscount = goodsDiscountService.selectById(goodsOrder.getDiscountId());
//			result.setGoodsDiscount(goodsDiscount);
		}

		return TableResult.OKResult(page);

	}

	@RequestMapping("/deleteGoodsOrder")
	@ResponseBody
	public TableResult deleteGoodsOrder(GoodsOrder goodsOrder) {
		goodsOrder.preUpdate();
		boolean b = goodsOrderService.deleteById(goodsOrder.getId());
		return TableResult.Result(b);
	}

}
